package ac.you.api;

import ac.you.api.ask.Ask;
import ac.you.api.ask.e.ResultHandler;
import ac.you.api.ask.i.PackageTasker;
import ac.you.api.chat.Tellraw;
import ac.you.api.chat.e.At;
import ac.you.api.chat.e.Cl;
import ac.you.api.chat.e.Ho;
import ac.you.api.nms.NMSApi;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class AcYouAPI extends JavaPlugin implements Listener, CommandExecutor {
    public static Plugin INSTANCE;
    public static String NMS_Version;
    @Override
    public void onEnable() {
        INSTANCE = this;
        NMS_Version = NMSApi.convertNMSVersion();
        try {
//            Command.main("acyou")
//                    .limit(new String[]{"ask","area"},null)
//                    .limit(new String[]{"create","list","remove","run","stop"},null)
//                    .only(Command.CommandType.PLAYER)
//                    .register();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Bukkit.getPluginManager().registerEvents(this,this);
    }

    @EventHandler(priority = EventPriority.LOW)
    public void test_playerjoin(PlayerJoinEvent event){
        Player e = event.getPlayer();
        Ask.who(e)
           .todo(tasker -> {
               for (LivingEntity livingEntity : tasker.getAsk().getAssociatedEntity()) {
                   Player p = (Player) livingEntity;
                   Tellraw.create().text("随机传送?").tellraw().send(p);
                   Tellraw.create().text("[同意]").hover(Ho.SHOW_TEXT,"点击同意").click(At.COPY,"ok").tellraw().text("   [不同意]").hover(Ho.SHOW_TEXT,"点击不同意").click(At.COPY,"no").tellraw().send(p);
               }
               return true;
           }, () -> {},"teleport", new PackageTasker(){
               final Map<Player,Boolean> players = new HashMap<>();
               @EventHandler
               public void speak(AsyncPlayerChatEvent e){
                   if (hasEntityIn(e.getPlayer()) && !players.containsKey(e.getPlayer())){
                       players.putIfAbsent(e.getPlayer(), e.getMessage().equals("ok"));
                       if (players.size() >= getAsk().getAssociatedEntity().size()){
                           pack.pack("chose",players);
                           done();
                       } else {
                           Tellraw.create().text("请等待其他玩家选择").tellraw().send(e.getPlayer());
                       }
                       e.setCancelled(true);
                   }
               }
           }, ResultHandler.build((tasker, s) -> {
               try {
                   if (s != null){
                       Map<Player,Boolean> $a = s.get(Map.class, "chose");
                       $a.forEach((player, aBoolean) -> {
                           if(aBoolean){
                               Tellraw.create().text("传送成功").tellraw().send(player);
                               Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"tp " + player.getName() + " ~+4 ~+4 ~+5");
                           } else {
                               Tellraw.create().text("传送失败，你不同意此次传送").tellraw().send(player);
                           }
                       });
                   }
               }catch (Exception c){
                   c.printStackTrace();
               }
           }),(ask)->{}).start(30);

        AcYouAPI.noticeToPlayer(Tellraw.create().text("玩家").color(Cl.GOLD).tellraw()
                                        .text(" >> ").color(Cl.YELLOW).bold(true).tellraw()
                                        .text(e.getDisplayName()).color(Cl.DARK_GRAY).hover(Ho.SHOW_TEXT,"\u00A76点击复制名字").click(At.COPY,e.getDisplayName()).tellraw()
                                        .text(" << ").color(Cl.YELLOW).bold(true).tellraw()
                                        .text("加入服务器！").color(Cl.GOLD).tellraw());
    }


    public static void noticeToPlayer(Tellraw text){
        Tellraw.create()
                .text(" AcYouAPI ").color(Cl.YELLOW).bold(true).tellraw()
                .text(" = ").color(Cl.GRAY).obfuscated(true).tellraw()
                .text(text).send(false, Bukkit.getOnlinePlayers().toArray(new Player[]{}));
    }
    public static void log(Level level, String text){
        Logger.getLogger("[AcYouAPI]").log(level,text);
    }




    @Override
    public void onDisable() {
        Bukkit.getScheduler().cancelTasks(this);
    }



}
